<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;
use think\Exception;

/**
 * 申诉管理
 *
 * @icon fa fa-circle-o
 */
class Appeal extends Backend
{
    
    /**
     * Appeal模型对象
     * @var \app\admin\model\Appeal
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Appeal;
        $this->view->assign("typeList", $this->model->getTypeList());
        $this->view->assign("statusList", $this->model->getStatusList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->with(['user'])
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->with(['user'])
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            foreach ($list as $row) {
                
                $row->getRelation('user')->visible(['nickname','mobile','avatar']);
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
    /**
     * 买家胜诉
     */
    public function buyer($ids = null)
    {

        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        Db::startTrans();
        try {
            //买家胜诉,币到账
            $order = db('order')->find($row->order_id);
            if ($order['buy_user_id'] == $row->user_id){
                //申诉的人事买家
                $row->status = 2;//胜诉
            }else{
                $row->status = 3;//败诉诉
            }
            $row->updatetime = time();
            $row->save();
            db('order')->where('id',$row->order_id)->update(['status'=>3,'updatetime'=>time()]);//订单完成
            if ($order['type'] == 1){
                //cb
                db('user')->where('id',$order['buy_user_id'])->setInc('cb_num',$order['num']);
                setCbwater($order['buy_user_id'],$order['num'],1,'买入成功');

                db('user')->where('id',$order['sell_user_id'])->setDec('cb_freeze',$order['all_freeze']);


            }else{
                //cc
                db('user')->where('id',$order['buy_user_id'])->setInc('cc_num',$order['num']);
                setCbwater($order['buy_user_id'],$order['num'],1,'买入成功');

                db('user')->where('id',$order['sell_user_id'])->setDec('cc_freeze',$order['all_freeze']);
            }

            Db::commit();
        }catch (Exception $exception){
            Db::rollback();
            $this->error($exception->getMessage());
        }
        $this->success('ok');
    }
    /**
     * 卖家胜诉
     */
    public function seller($ids = null)
    {

        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        Db::startTrans();
        try {
            //买家败诉,币退回
            $order = db('order')->find($row->order_id);
            if ($order['buy_user_id'] == $row->user_id){
                //申诉的人事买家
                $row->status = 3;//败诉诉
            }else{
                $row->status = 2;//胜诉
            }
            $row->updatetime = time();
            $row->save();
            db('order')->where('id',$row->order_id)->update(['status'=>3,'updatetime'=>time()]);//订单完成
            if ($order['type'] == 1){
                //cb
                db('user')
                    ->where('id',$order['sell_user_id'])
                    ->inc('cb_num',$order['all_freeze'])
                    ->dec('cb_freeze',$order['all_freeze'])
                    ->update();
                setCbwater($order['sell_user_id'],$order['all_freeze'],1,'订单退回');

            }else{
                //cc
                db('user')
                    ->where('id',$order['sell_user_id'])
                    ->inc('cc_num',$order['all_freeze'])
                    ->dec('cc_freeze',$order['all_freeze'])
                    ->update();
                setCcwater($order['sell_user_id'],$order['all_freeze'],1,'订单退回');
            }

            Db::commit();
        }catch (Exception $exception){
            Db::rollback();
            $this->error($exception->getMessage());
        }
        $this->success('ok');
    }
}
